function initPage(){
	countDenuncias();	

	var favPunt = document.getElementById("puntFavour");
	var user= document.getElementById("usrLi");
	var userFav= document.getElementById("myFavour");
	var listDenuncias= document.getElementById("listaDenuncias");
	listDenuncias.onclick=showListDenuncias;
	favPunt.onclick=showFavPunt;
	userFav.onclick=showMyFavours;
	user.onclick=cargarPerfil;
	countFavPorPuntuar();
}

function showMyFavours(){
	request=createRequest();
	if (request==null){
		alert("Unnable to create the request");
	}else{
		var url="./myFavours.php";
		request.onreadystatechange= function()
		{
			if (request.readyState==4 && request.status==200)
			{
				document.getElementById("home").className="";
				var li= document.getElementById("favLi");
				li.className="active";
				document.getElementById("rowCanvas").innerHTML= request.responseText;

			}
		};
		request.open("GET",url,true);
		request.send();
	}
}

function cargarPerfil(){
	request=createRequest();
	if (request==null){
		alert("Unnable to create the request");
	}else{
		var url="./userProfile.php";
		request.onreadystatechange= function()
		{
			if (request.readyState==4 && request.status==200)
			{

				var li= document.getElementById("usrLi");
				li.className="active";
				document.getElementById("main").innerHTML= request.responseText;

			}
		};
		request.open("GET",url,true);
		request.send();
	}
}

function loginUser(){

	if(document.form.email==null||document.form.pswd.value==null){
		alert("Debe rellenar los dos campos");
	}else{
		document.form.action="./php/newSession.php";
		document.form.method="POST";
		document.form.submit();
	}
}

function deleteFavour(id){
	request=createRequest();
	if (request==null){
		alert("Unnable to create the request");
	}else{
		var div="#my"+id;
		var url="./deleteFav.php?idFav="+id;
		request.onreadystatechange= function(){
			if (request.readyState==4 && request.status==200){	

				if(request.responseText=="1"){
					$(div).hide("slow");
				}else{
					alert(request.responseText);
					alert("No se ha podido eliminar el favor");

				}



			}
		};
		request.open("GET",url,true);
		request.send();
	}
}

function addPhoto(){
	document.favPhoto.submit();
}

function showFavPunt(){

	request=createRequest();
	if (request==null){
		alert("Unnable to create the request");
	}else{
		var url="./puntFavours.php";
		request.onreadystatechange= function()
		{
			if (request.readyState==4 && request.status==200)
			{

				document.getElementById("main").innerHTML= request.responseText;

			}
		};

		request.open("GET",url,true);
		request.send();
	}
}

function showFavour(id){
	request=createRequest();

	if(request==null){
		alert("Unnable to create the request");
	}else{
		var url="./showFavour.php?idFav="+id;
		request.onreadystatechange=function(){
			if (request.readyState==4 && request.status==200){
				document.getElementById("rowCanvas").innerHTML= request.responseText;
			}
		};

		request.open("GET",url,true);
		request.send();
	}
}

function showComents(id){
	$("#askHeader").hide();
	$("#commentsHeader").show();
	$("#sendComment").hide();
	$("#commentsFooter").show();
	request=createRequest();

	if(request==null){
		alert("Unnable to create the request");
	}else{
		url="./showComent.php?idFav="+id;
		request.onreadystatechange=function(){
			if(request.readyState==4 && request.status==200){
				document.getElementById("comentsBody").innerHTML=request.responseText;
				$("#comentsBody").fadeIn("slow");

			}
		};
		request.open("GET",url,true);
		request.send();
	}

}

function addComent(){
	$("#commentsUserFooter").hide();
	$("#newComent").show("slow");
	$("#showNewComent").hide();
	$("#sendComment").show("slow");
	$("#sendComment").click(function(){
		$.post("addComment.php",$("#nuevoComentario").serialize(),function(res){
			if (res!=0){
				$("#comentsBody").fadeOut("slow");
				$("#showNewComent").show();
				showComents(res);
			}else{
				alert("No se ha podido añadir el comentario");
			}


		});

	});

}

function askForFavor(id){

	$("#askHeader").show();
	$("#commentsHeader").hide();
	$("#commentsFooter").hide();
	$("#askFooter").show();
	request=createRequest();

	if(request==null){
		alert("Unnable to create the request");
	}else{
		url="./verify.php?idFav="+id;
		request.onreadystatechange=function(){
			if(request.readyState==4 && request.status==200){
				document.getElementById("comentsBody").innerHTML=request.responseText;
				$("#comentsBody").fadeIn("slow");
				$("#exitoFav").hide();

			}
		};
		request.open("GET",url,true);
		request.send();
	}


}

function ask(){


	$.post("askFavour.php",$("#askForm").serialize(),function(res){

		if (res==0){
			$("#verify").fadeOut("slow");
			$("#exitoFav").show("slow");
			$("#askButton").hide();
			countFavPorPuntuar()

		}else{
			alert(res);
		}


	});


}

function puntuarFavor(id){
	$("#puntExito").hide();
	var sID= id.toString();
	$('input[name=idFav]').val(sID);
	$("#send").click(function(){
		$.post("puntuar.php",$("#puntuacion").serialize(),function(res){

			if (res==0) {

				var div="#punt"+id;
				$("#puntuacion").hide();
				$("#puntExito").show();
				$(div).hide("slow");
				$("#send").hide();
				countFavPorPuntuar()
			}else{
				alert(res);
			}

		});

	});
}

function showUserComents(id){

	$("#showNewUserComent").show();
	$("#sendUserComment").hide();

	request=createRequest();

	if(request==null){
		alert("Unnable to create the request");
	}else{
		url="./showUserComment.php?idUsr="+id;
		request.onreadystatechange=function(){
			if(request.readyState==4 && request.status==200){
				document.getElementById("userComentsBody").innerHTML=request.responseText;
				$("#comentsBody").fadeIn("slow");

			}
		};
		request.open("GET",url,true);
		request.send();
	}
}

function addUserComent(){
	$("#newUserComent").show("slow");
	$("#showNewUserComent").hide();
	$("#sendUserComment").show("slow");
	$("#sendUserComment").click(function(){
		alert("dentro");
		$.post("addUserComment.php",$("#nuevoComentarioUsuario").serialize(),function(res){
			alert(res);
			if (res>=0){
				$("#comentsBody").fadeOut("slow");
				showUserComents(res);
				$("#showNewUserComent").show();
			}else{
				alert("No se ha podido añadir el comentario");
			}


		});

	});
}

function editFav(id){
	request=createRequest();

	if(request==null){
		alert("unnable to create request");
	}else{
		url="./editFav.php?idFav="+id;

		request.onreadystatechange=function(){
			if(request.readyState==4 && request.status==200){
				document.getElementById("rowCanvas").innerHTML=request.responseText;


			}
		};

		request.open("GET", url, true);
		request.send();

	}

}
function aply(){
	if (validarEdit()==true){
		$.post("edit.php",$("#favEditForm").serialize(),function(res){

			if(res>=0){
				alert("Los cambios se han realizado con éxito");
				editFav(res);
			}else{
				alert(res);
			}

		});
	}
}

function editFavFoto(id){
	$('input[name=idChangeFav]').val(id);
	$("#changeFavPhoto").click(function(){
		document.changeFavPhoto.method="POST";
		document.changeFavPhoto.action="changePhoto.php";
		document.changeFavPhoto.submit();
	});
}

function aplyUsr(){
	$.post("editUsr.php",$("#usrEditForm").serialize(),function(res){
		if (res==0) {
			cargarPerfil;
			alert("Los cambios han sido realizados");
		}else{
			alert(res);
		}

	});
}

function denunciarFavor(id){

	var sID= id.toString();
	$('input[name=idFav]').val(sID);
	$("#sendDenuncia").show();
	$("#denunciaForm").fadeIn();
	$("#denunciaExito").hide();
	$("#denunciaFracaso").hide();
	var sID= id.toString();
	$('input[name=idFavDen]').val(sID);
	$("#sendDenuncia").click(function(){
		$.post("addDenuncia.php",$("#denunciaForm").serialize(),function(res){
			if(res==0){
				var div="#punt"+id;
				$(div).hide("slow");
				$("#sendDenuncia").hide();
				$("#denunciaForm").fadeOut();
				$("#denunciaExito").show();
				countFavPorPuntuar()
			}else{
				alert(res);
				$("#sendDenuncia").hide();
				$("#denunciaForm").fadeOut();
				$("#denunciaFracaso").show();
			}
		});
	});
}

function showListDenuncias(){
	request=createRequest();

	if (request==null){
		alert("Unnable to create request");
	}else{
		url="./listDenuncias.php";

		request.onreadystatechange=function(){

			if(request.readyState==4 && request.status==200){
				document.getElementById("main").innerHTML=request.responseText;
			}

		};

		request.open("GET",url,true);
		request.send();
	}
}

function showDenuncias(id){
	request=createRequest();
	var sID= id.toString();
	$('input[name=hiddenIdDenunciado]').val(sID);

	if (request==null){
		alert("Unnable to create request");
	}else{
		url="./showDenuncias.php?idFav="+id;

		request.onreadystatechange=function(){

			if(request.readyState==4 && request.status==200){

				document.getElementById("denunciasModalbody").innerHTML=request.responseText;
			}

		};

		request.open("GET",url,true);
		request.send();


	}
}

function deleteReportedFavour(){
	$.post("deleteDenunciedFav.php", $("#listDenunciaForm").serialize(),function(res){
		if(res>=0){
			alert("El favor denunciado ha sido eliminado del sistema.");
			var denDiv= "#den"+res;
			$(denDiv).hide();
		}else{
			alert(res);
		}
	});
}

function countDenuncias(){
	request=createRequest();

	if(request==null){
		alert("Unnable to create request");
	}else{
		url="./countDenuncias.php";

		request.onreadystatechange=function(){
			if(request.readyState==4 && request.status==200){

				document.getElementById("countDen").innerHTML=request.responseText;
			}
		};

		request.open("GET", url, true);
		request.send();
	}
}

function countFavPorPuntuar(){
	request=createRequest();

	if(request==null){
		alert("Unnable to create request");
	}else{
		var url="./countFav.php";

		request.onreadystatechange=function(){
			if(request.readyState==4 && request.status==200){

				document.getElementById("userFavCount").innerHTML=request.responseText;
			}
		};

		request.open("GET", url, true);
		request.send();
	}
}

function addCat(){
	$.post("addCategoria.php",$("#formAddCat").serialize(),function(res){

		if (res==0){

			recargarLista();
		}else{
			alert(res);
		}

	});
}

function recargarLista(){

	request= createRequest();

	if (request==null){
		alert("Unnable to create the request");
	}else{

		url="./listCategorias.php";

		request.onreadystatechange=function(){
			if (request.readyState==4 && request.status==200){
				document.getElementById("categoriasExistentes").innerHTML=request.responseText;
			}
		};
		request.open("GET", url, true);
		request.send();

	}

}

function deleteCat(id){

	request=createRequest();

	if(request==null){
		alert("Unnable to create the request");
	}else{
		url="deleteCat.php?idCat="+id;

		request.onreadystatechange=function(){
			if(request.readyState==4 && request.status==200){

				if (request.responseText=="0"){
					recargarLista();
				}else{
					alert("Error al eliminar la categoría");
				}	
			}
		};
		request.open("GET", url, true);
		request.send();
	}
}

function search(){

	$.post("showSearchedResult.php",$("#favForm").serialize(),function(res){

		document.getElementById("mainContainer").innerHTML= res;

	});
}

function createFav(){
	if(validateFav){
		$.post("addFav.php",$("#newFavForm").serialize(),function(res){

			$("#favFormContainer").fadeOut("slow");
			if (res>=0) {
				$("#exito").delay(500).fadeIn("slow");
				$("#favFooter").hide();
				$("#photoFooter").show();
				$("#idHidden").attr('value', res);

			}else{
				$("#createFavBut").hide();
				$("#fracaso").delay(500).fadeIn("slow");

			}
		});
	}else{
		alert("No ha completado el formulario debidamente");
	}
}

function refreshfavModal(){
	$("#createFavBut").show();
	$("#fracaso").hide();
	$("#favFormContainer").show();
	$("#favFooter").show();
	$("#photoFooter").hide();
}

function sendOAuthForm(){
	if(!validarForm){
		alert("Debe rellenar los campos obligatorios");
	}else{
		document.OAuthForm.action="addOAuthUser.php";
		document.OAuthForm.method="POST";
		document.OAuthForm.submit();
	}

}

function checkImage(fileImg){

	img = fileImg.value;
	valid = new Array(".gif", ".jpg", ".png");
	if (img != "") {
		ext = (img.substring(img.lastIndexOf("."))).toLowerCase();
		check = false;
		for (var i = 0; i < valid.length; i++) {
			if (valid[i] == ext) {
				check = true;
				break;
			}
		}
		if (!check) {
			alert(ext + " La extensión del archivo de imagen no es válida!");
			fileImg.value="";			
		}
	}
}

function validarForm(){
	if($("#emailOA").value==""){
		return false;
	}
	
	if($("#datosOA").value==""||$("#datosOA").length<30){
		return false;
	}
	
	if($("#estudiosOA").value=="" || $("#datosOA").length<30){
		return false;
	}
	
	return true;
}

function UpdateUserPhoto(){
	document.changeUsrPhoto.action="changeUserPhoto.php";
	document.changeUsrPhoto.method="POST";
	document.changeUsrPhoto.submit();
}
